<?php
	/**
	 * 网页抓取类
	 * detail description
	 * @todo
	 * @author swingcoder <swingcoder@msn.com>
	 * @version 0.1
	 * @copyright Copyright (c) 2009, swingcoder.com
	 */

	require_once '../lib/http.php';

	class snap
	{
		public $charset = 'utf-8';
		public $content_type = 'text/html';
		private $target_url;
		private $uri = '';

		function snap($uri)
		{
			$this->uri = $uri;
		}

		function run()
		{
			$result = $this->send( $this->uri );

			//follow redirection
			if( substr($result, 0, 3) == '302' )
			{
				$location = substr($result, strpos($result, '|')+1 );
				$result = $this->send($location);
			}

			include_once 'filter.php';
			$filter = new filter();
			$link = $filter->cleanup($result, $this->uri);

			return $this->html($link, $this->charset);
		}

		function send($uri)
		{
			$this->uri = $uri;
			$obj = new http($uri,'utf-8','utf-8');
			$obj->set_command(strtolower($_SERVER['REQUEST_METHOD']) == 'get' ? $_GET : $_POST, false);
			$obj->send();
			$obj->close();

			$this->content_type = $obj->response_content_type;
			$this->charset = $obj->response_charset;
			$this->target_url = $obj->target_url;

			return $obj->result;
		}

		function html( $link, $charset )
		{
			$link = array_merge($link, array('charset'=>$this->charset, 'content_type'=>$this->content_type) );

			return $link;
		}

	}
?>